﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace USMCdotNET
{
    sealed class Loop:ILoopElement
    {
        bool atprocess = false;
        bool accomplished = false;
        bool returnToStart = false;
        Quantity iterationsNumber;
        PathCollection path;

        public Loop(bool ret, bool inf, int ct)
        {
            path = new PathCollection();
            iterationsNumber = new Quantity() { Count = ct, Infinite = inf };
            returnToStart = ret;
        }
        public void Save(BinaryWriter b)
        {
            b.Write("Loop");
            b.Write(returnToStart);
            b.Write(iterationsNumber.Count);
            b.Write(iterationsNumber.Infinite);
            path.Save(b);
            b.Write("end_Loop");
        }
        public void Move()
        {
            if (returnToStart) 
            {
                if (iterationsNumber.Infinite)
                {
                    atprocess = true;
                    do
                    {
                        path.Move();
                        path.LineCollection[0].Return();
                    }
                    while (iterationsNumber.Infinite);
                    atprocess = false;
                    accomplished = true;
                }
                else
                {
                    atprocess = true;
                    for (int i = -1; ++i <= (iterationsNumber.Count - 1); )
                    {
                        path.Move();
                        path.LineCollection[0].Return();
                    }
                    atprocess = false;
                    accomplished = true;
                }
            }
            else//???
            {
                path.ShiftCount();
                if (iterationsNumber.Infinite)
                {
                    atprocess = true;
                    do
                    {
                        path.Move();
                        foreach (BaseLine o in path.LineCollection)
                        {
                            o.Destination.Change(o.Destination.X + path.Shift[0], o.Destination.Y + path.Shift[1]);
                        }
                    }
                    while (iterationsNumber.Infinite);
                    atprocess = false;
                    accomplished = true;
                }
                else
                {
                    atprocess = true;
                    for (int i = -1; ++i <= (iterationsNumber.Count - 1); )
                    {
                        path.Move();
                        foreach (BaseLine o in path.LineCollection)
                        {
                            o.Destination.Change(o.Destination.X + path.Shift[0], o.Destination.Y + path.Shift[1]);
                        }
                    }
                    atprocess = false;
                    accomplished = true;
                }
            }
        }
        public PathCollection Path { get { return path; } set { path = value; } }
        public bool AtProcess { get { return atprocess; } }
        public bool Accomplished { get { return accomplished; } }
        public bool ReturnToStart { get { return returnToStart; } set { returnToStart = value; } }
        public Quantity IterationsNumber { get { return iterationsNumber; } set { iterationsNumber = value; } }
    }
}
